// pages/mine/certification.js

const db = wx.cloud.database().collection('User')
const dbA = wx.cloud.database().collection('Admin')
const dbW = wx.cloud.database().collection('Worker')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    info:{},
    name:'',
    number:'',
    phoneNumber:'',
    class:'',
    sub:[],
    key:"",
    radio:'0',
    Sshow:false,
    Wshow:false,
    Ashow:false
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  subonChange(event) {
    this.setData({
      sub: event.detail,
    });
  },
  /**
   * 认证按钮
   */
  submit:function(){
    wx.getUserProfile({
      desc: '展示用户信息',
      success: (res) => {
        this.setData({
          info:res.userInfo
        })
        if(this.data.radio == '0'
    || ((this.data.name === '' || this.data.number === '' || this.data.class === '') && this.data.radio == 'S') 
    || (this.data.radio == 'A' && this.data.key === '')
    || ((this.data.name === '' || this.data.number === '' || this.data.sub.length == 0|| this.data.class === '') && this.data.radio == 'W')){
      wx.showModal({
        title: '提示',
        content: '你有未填入的内容',
      })
    }else{
      //若是学生
      if(this.data.radio == 'S'){
        db.add({
          data:{
            url:this.data.info.avatarUrl,
            name:this.data.name,
            number:this.data.number,
            class:this.data.class,
            identity:this.data.radio
          },
          success:function(res){
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 2000//持续的时间
            })
  
            var start=Date.now();
            while(true){
            var nowTime=Date.now();
            var offset=nowTime-start;
            if(offset>=2000)
              break;
  }
            wx.switchTab({
              url: 'mine',
              })
          }
        })
      }
      //若是义工
      else if(this.data.radio == "W"){
        dbW.where({
          name:this.data.name,
          number:this.data.number,
          used:false
        }).get().then(res =>{
          console.log(res.data.length)
          if(res.data.length == 0){
            wx.showModal({
              title: '提示',
              content: '身份认证未通过',
            })
          }else{
            dbW.where({
              name:this.data.name,
              number:this.data.number
            }).update({
              data:{
                used:true
              }
            })

            db.add({
              data:{
                url:this.data.info.avatarUrl,
                name:this.data.name,
                number:this.data.number,
                phoneNumber:this.data.phoneNumber,
                class:this.data.class,
                identity:this.data.radio,
                sub:this.data.sub
              },
              success:function(res){
                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 2000//持续的时间
                })
      
                var start=Date.now();
                while(true){
                var nowTime=Date.now();
                var offset=nowTime-start;
                if(offset>=2000)
                  break;
      }
                wx.switchTab({
                  url: 'mine',
                  })
              }
            })
          }
        })
      }
      //若是管理员
      else{
        dbA.where({
          key:this.data.key
        }).get().then(res=>{
          console.log(res.data)
          if(res.data.length == 0 || res.data[0].used){
            wx.showModal({
              title: '提示',
              content: '身份码无效',
            })
          }else{
            dbA.where({
              key:this.data.key
            }).update({
              data:{
                key:this.data.key,
                used:true
              },
              success(res){
                console.log("修改成功", res)
              },
              fail(res){
                console.log("修改失败", res)
              }
            })
            db.add({
              data:{
                key:this.data.key,
                identity:this.data.radio
              },
              success:function(res){
                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 2000//持续的时间
                })
      
                var start=Date.now();
                while(true){
                var nowTime=Date.now();
                var offset=nowTime-start;
                if(offset>=2000)
                  break;
                }
                wx.switchTab({
                  url: 'mine',
                  })
              }
            })
          }
        })
      }
      
    }
      },
    })
  },


  /**
   * bindinput 自定义
   */
  namebindinput:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  numberbindinput:function(e){
    this.setData({
      number:e.detail.value
    })
  },
  phonenumberbindinput:function(e){
    this.setData({
      phoneNumber:e.detail.value
    })
    // console.log(this.data.phoneNumber)
  },
  classbindinput:function(e){
    this.setData({
      class:e.detail.value
    })
  },
  keybindinput:function(e){
    this.setData({
      key:e.detail.value
    })
  },
  onChange(event) { 
    this.setData({ 
      radio: event.detail, 
    }); 
    if(this.data.radio == 'A'){
      this.setData({ 
        name:'',
        number:'',
        class:'',
        sub:[],
        Sshow:false,
        Wshow:false,
        Ashow:true
      }); 
    }else if(this.data.radio == 'S'){
      this.setData({ 
        key:'',
        sub:[],
        Sshow:true,
        Wshow:false,
        Ashow:false
      }); 
    }else{
      this.setData({ 
        key:'',
        Sshow:false,
        Wshow:true,
        Ashow:false
      }); 
    }
  }, 
})